package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teess01Supplier;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class SupplierFind implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        Teess01Supplier supplier = (Teess01Supplier) parameters[0];
        String code = null;
        String name = null;
        String classCode = null;
        Teesg01Plant plant = null;
        
        code = supplier.getEess01SupplierCodeC();
        name = supplier.getEess01SupplierNameN();
        classCode = supplier.getEess01SupplierClassCodeC();
        plant = supplier.getEess01PlantCodeC();

        if (name == null) {
            name = "%";
        } else {
            name += "%";
        }

        if (code.equals("")) {
            code = "%";
        } else {
            code += "%";
        }
        
        exp = builder.get("eess01PlantCodeC").equal(plant);
        if (!classCode.equals("-1")){
            exp = exp.and(builder.get("eess01SupplierClassCodeC").equal(classCode));
        }
        if (code != null && !code.equals("")) {
            exp = exp.and(builder.get("eess01SupplierCodeC").toLowerCase().like(code.toLowerCase()));
        } 
        exp = exp.and(builder.get("eess01SupplierNameN").toLowerCase().like(
                    name.toLowerCase()));
        
        return exp;
    }

}
